<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\user\controller;

use cmf\controller\RestUserBaseController;
use think\Db;
use think\Validate;

//技术控制器
class TechnicianController extends RestUserBaseController
{


    //  ;
    /**
     * 添加企业注册登记表
     * require project_id项目id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * retuen code 1
     */
    public function upRegister()
    {
        $role = $this->getUserRole();   //  获取该用户角色
        $role = '技术员';
        if ($role != '技术员') $this->error("非技术员不能添加!");

        $validate = new Validate([
            'project_id' => 'require',

        ]);

        $validate->message([
            'project_id.require' => '请输入项目id!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        if ($data['product']) {
            $data['product'] = json_encode($data['product']);
        }

        if ($data['register_type']) {
            $data['register_type'] = json_encode($data['register_type']);
        }

        if (isset($data['register_stock_civics'])) {

            $register_stock_civics = $data['register_stock_civics'] ? $data['register_stock_civics'] : false;
            unset($data['register_stock_civics']);

        } else {
            $register_stock_civics = false;
        }
        if (isset($data['register_stock_legal'])) {

            $register_stock_legal = $data['register_stock_legal'] ? $data['register_stock_legal'] : false;
            unset($data['register_stock_legal']);

        } else {
            $register_stock_legal = false;
        }



        $register_update_id = Db::name('project_register')
            ->where('project_id', $data['project_id'])
            ->update($data);


        if ($register_update_id) {
            //添加如果公司名称字段真就添加公司名称到 preject
            $project_data['company_name'] = $data['company_name'];
            Db::name('project')
                ->where('id', $data['project_id'])
                ->update($project_data);
        }

        $project_register_data = Db::name('project_register')
            ->where('project_id', $data['project_id'])
            ->find();


        $register_id = $project_register_data['id'];
        //查出register_stock_civics 原来数据
        $register_stock_civics_log_data = Db::name('register_stock_civics')
            ->where('register_id', $register_id)
            ->select()
            ->toArray();

        //如果存在就删除
        if ($register_stock_civics_log_data) {
            //删除原来的 register_stock_civics表的数据 添加新的数据
            $num = Db::name('register_stock_civics')
                ->where('register_id', $register_id)->delete();
        }

        if ($register_stock_civics) {
            // 假就直接添加新的数据
            //$register_stock_civics = htmlspecialchars_decode($register_stock_civics);
            //$register_stock_civics = json_decode($register_stock_civics);

            $new_register_stock_civics = [];
            foreach ($register_stock_civics as $key => &$val) {
                //$val                             = $this->object_to_array($val);
                if ($val) {
                    $val['register_id']              = $register_id;
                    $new_register_stock_civics[$key] = $val;
                }

            }
            unset($val);

            $civics_num = Db::name('register_stock_civics')->insertAll($new_register_stock_civics);
        }


        $register_stock_legal_log_data = Db::name('register_stock_legal')
            ->where('register_id', $register_id)
            ->select()
            ->toArray();


        //如果存在就删除
        if ($register_stock_legal_log_data) {
            //删除原来的 register_stock_civics表的数据 添加新的数据
            $num = Db::name('register_stock_legal')->where('register_id', $register_id)->delete();

        }
        if ($register_stock_legal) {
            // 假就直接添加新的数据
           // $register_stock_legal = htmlspecialchars_decode($register_stock_legal);
            //$register_stock_legal = json_decode($register_stock_legal);
            $new_register_stock_legal = [];
            foreach ($register_stock_legal as $key => &$val) {
                //$val                            = $this->object_to_array($val);
                if ($val) {
                    $val['register_id']             = $register_id;
                    $new_register_stock_legal[$key] = $val;
                }

            }
            unset($val);

            $legal_num = Db::name('register_stock_legal')->insertAll($new_register_stock_legal);
        }


        $this->success("修改企业注册登记成功!");


    }


    /**
     * 对象 转 数组
     *
     * @param object $obj 对象
     * @return array
     */
    function object_to_array($obj)
    {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)object_to_array($v);
            }
        }

        return $obj;
    }


    //  添加项目之主情况表;
    public function upCondition()
    {
        $role = $this->getUserRole();   //  获取该用户角色
        $role = '技术员';
        if ($role != '技术员') $this->error("非审核员不能添加!");

        $validate = new Validate([
            'project_id' => 'require',

        ]);

        $validate->message([
            'project_id.require' => '请输入项目id!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        Db::name('project_condition')
            ->where('project_id', $data['project_id'])
            ->update($data);


        $this->success("修改主情况成功!");


    }

    //  更新知识产权汇总表;
    public function upKnowledge()
    {


        $role = $this->getUserRole();   //  获取该用户角色
        $role = '技术员';
        if ($role != '技术员') $this->error("非审核员不能添加!");

        $validate = new Validate([
            'project_id' => 'require',

        ]);

        $validate->message([
            'project_id.require' => '请输入项目id!',
        ]);


        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $data['one_invent_patent'] = $data['one_invent_patent'] ? json_encode($data['one_invent_patent']): '';
        $data['one_plant_variety'] = $data['one_plant_variety'] ? json_encode($data['one_plant_variety']): '';
        $data['one_defence_variety'] = $data['one_defence_variety'] ? json_encode($data['one_defence_variety']): '';
        $data['one_medicine_variety'] = $data['one_medicine_variety'] ? json_encode($data['one_medicine_variety']): '';
        $data['one_crop_variety'] = $data['one_crop_variety'] ? json_encode($data['one_crop_variety']): '';
        $data['one_new_medicine'] = $data['one_new_medicine'] ? json_encode($data['one_new_medicine']): '';
        $data['one_circuit_design'] = $data['one_circuit_design'] ? json_encode($data['one_circuit_design']): '';
        $data['two_software_copyright'] = $data['two_software_copyright'] ? json_encode($data['two_software_copyright']): '';
        $data['two_facade_variety'] = $data['two_facade_variety'] ? json_encode($data['two_facade_variety']): '';









        if (isset($data['knowledge_info'])) {

            $knowledge_info = $data['knowledge_info'] ? $data['knowledge_info'] : false;
            unset($data['knowledge_info']);

        } else {

            $knowledge_info = false;
        }



        if (isset($data['knowledge_condition'])) {
            $knowledge_condition = $data['knowledge_condition'] ? $data['knowledge_condition'] : false;

            unset($data['knowledge_condition']);

        } else {
            $knowledge_condition = false;
        }




        $id = Db::name('project_knowledge')
            ->where('project_id', $data['project_id'])
            ->update($data);




        $project_knowledge_data = Db::name('project_knowledge')
            ->where('project_id', $data['project_id'])
            ->find();




        $knowledge_id = $project_knowledge_data['id'];

        //knowledge_info 原来数据
        $knowledge_info_log_data = Db::name('knowledge_info')
            ->where('knowledge_id', $knowledge_id)
            ->select()
            ->toArray();

        //knowledge_condition 原来数据
        $knowledge_condition_log_data = Db::name('knowledge_condition')
            ->where('knowledge_id', $knowledge_id)
            ->select()
            ->toArray();

        //如果存在就删除

        if ($knowledge_info_log_data) {

            //删除原来的 knowledge_info表的数据 添加新的数据
            $num = Db::name('knowledge_info')
                ->where('knowledge_id', $knowledge_id)
                ->delete();

        }

        //如果存在就删除
        if ($knowledge_condition_log_data) {

            //删除原来的 knowledge_info表的数据 添加新的数据
            $num = Db::name('knowledge_condition')
                    ->where('knowledge_id', $knowledge_id)
                    ->delete();

        }



        if ($knowledge_info) {
            //$knowledge_info     = htmlspecialchars_decode($knowledge_info);
            //$knowledge_info     = json_decode($knowledge_info);
            $new_knowledge_info = [];

            foreach ($knowledge_info as $key => &$val) {
               // $val                      = $this->object_to_array($val);
                if ($val) {
                    $val['knowledge_id']      = $knowledge_id;
                    $val['info_accessory']    = $val['info_accessory'] ? json_encode($val['info_accessory']) : '';
                    $new_knowledge_info[$key] = $val;
                }

            }
            unset($val);

            $civics_num = Db::name('knowledge_info')->insertAll($new_knowledge_info);

        }



        if ($knowledge_condition) {

            //$knowledge_condition     = htmlspecialchars_decode($knowledge_condition);
            //$knowledge_condition     = json_decode($knowledge_condition);
            $new_knowledge_condition = [];


            foreach ($knowledge_condition as $key => &$val) {
                //$val                           = $this->object_to_array($val);
                if ($val) {
                    $val['knowledge_id']           = $knowledge_id;
                    $val['condition_accessory']    = $val['condition_accessory'] ? json_encode($val['condition_accessory']) : '';
                    $new_knowledge_condition[$key] = $val;
                }
            }
            unset($val);


            $legal_num = Db::name('knowledge_condition')->insertAll($new_knowledge_condition);
        }


        $this->success("添加主情况成功!");


   //    [{"knowledge_id":"3","intellectual_property_number":"3","intellectual_property_name":"知识产权名称","knowledge_info_type_id":"1","privileged_time":"授权时间","privileged_num":"授权号","access":"获取方式","intellectual_property_status":"0","info_accessory":"附件"}]
//        [{"knowledge_id":"3","standard_name":"标准名称","standard_level_id":"知识产权名称","standard_num":"标准编号","standard_time":"标准时间","participation_way":"参与方式","condition_accessory":"附件"}]


    }


}
